/*
 * @Author: HanYaJun
 * @Date: 2023-01-04 11:45:11
 * @Email: hanyajun@wedobest.com.cn
 * @Description: 眼力检测
 */

const trace = function (...args) {
    console.log('Main【HYJ】', ...args);
}

const traceError = function (...args) {
    console.error('Main【HYJ】', ...args);
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class Main extends cc.Component {
    /**方块背景 */
    private bgLayout: cc.Node = null;

    /**
     * @description: 当附加到一个激活的节点上或者其节点第一次激活时候调用
     */
    protected onLoad(): void {
        // 方块背景
        this.bgLayout = cc.find('loginPage/background/top/blockSprite/bgLayout', this.node);
        this.loadPrefab();
        this.fullScreenAdapter();
    }

    /**
     * @description: 加载方块预制
     * @param {function} finish
     */
    private loadPrefab(): void {
        // 方块预制地址
        let blockStr: string = 'home/colorLump';
        // 方块预制获取
        cc.loader.loadRes(blockStr, cc.Prefab, (error: Error, resource: any) => {
            if (!error) {
                trace('方块预制加载成功, this.bgLayout:', this.bgLayout, 'resource:', resource);
                let initCount: number = 9;
                for (let i = 0; i < initCount; i++) {
                    if (i === 1) {
                        // 创建节点
                        let enemy: cc.Node = cc.instantiate(resource);
                        enemy.color = cc.Color.BLACK.fromHEX('#00CD00');
                        enemy.parent = this.bgLayout;
                    } else {
                        // 创建节点
                        let enemy: cc.Node = cc.instantiate(resource);
                        enemy.parent = this.bgLayout;
                    }
                }
            } else {
                traceError('error:', error);
            }
        });
    }

    /**
     * @description: 点击事件
     * @param {cc} event
     */
    public clickEvent(event: cc.Event.EventTouch): void {
        let name = event.target.name;
        switch (name) {
            case 'startGame': {
                let loginPage: cc.Node = this.node.getChildByName("loginPage");
                loginPage.active = false;
                break;
            }
            default: {
                break;
            }
        }
    }

    /**
     * @description: 屏幕适配
     */
    private fullScreenAdapter(): void {
        // 监听窗口大小变化时的回调，每次窗口变化都要自动适配
        cc.view.setResizeCallback(() => {
            // 当前屏幕分辨率比例
            let screenRatio: number = cc.winSize.width / cc.winSize.height;
            // 设计稿分辨率比例
            let designRatio: number = cc.Canvas.instance.designResolution.width / cc.Canvas.instance.designResolution.height;
            if (screenRatio <= 1) {
                // 屏幕高度大于或等于宽度,即竖屏
                if (screenRatio <= designRatio) {
                    this.setFitWidth();
                } else {
                    // 此时屏幕比例大于设计比例
                    // 为了保证纵向的游戏内容不受影响，应该使用 fitHeight 模式
                    this.setFitHeight();
                }
            } else {
                //屏幕宽度大于高度,即横屏
                this.setFitHeight();
            }
        });
    }

    private setFitWidth() {
        cc.Canvas.instance.fitHeight = false;
        cc.Canvas.instance.fitWidth = true;
    }

    private setFitHeight() {
        cc.Canvas.instance.fitHeight = true;
        cc.Canvas.instance.fitWidth = false;
    }

    /**
     * @description: 当该组件被销毁时调用
     */
    protected onDestroy(): void {

    }
}
